/**
 * Copyright (c) 2010 XXXXX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.edu.itba.ia2010.geneticos.motor.api.impl;

import java.util.Random;

import ar.edu.itba.ia2010.geneticos.motor.api.Gen;
import ar.edu.itba.ia2010.geneticos.motor.api.MotorGeneticoContext;

/**
 * Gen booleano. Es inmutable para mejorar el almacenamiento en memoria.
 * 
 * @author Juan F. Codagnone
 * @since Mar 21, 2010
 */
public class BooleanGen implements Gen<Boolean> {
    private final boolean value;
    
    /**
     * La visibilidad de paquete es para fomentar el uso de 
     * {@link BooleanGen#TRUE_GEN} y {@link BooleanGen#FALSE_GEN}  
     */
    BooleanGen() {
        this(false);
    }
    
    /**
     * La visibilidad de paquete es para fomentar el uso de 
     * {@link BooleanGen#TRUE_GEN} y {@link BooleanGen#FALSE_GEN}  
     */
    BooleanGen(final boolean value) {
        this.value = value;
    }

    /** @see Gen#mutar(Random, double) */
    public final Gen<Boolean> mutar(final Random random, 
            final double probabilidad, final MotorGeneticoContext ctx)  {
        Gen<Boolean> ret = null;
        if(random.nextDouble() <= probabilidad) {
            ret = flip();
        }
        return ret;
    }

    /** cambia el valor de la  */
    public final BooleanGen flip() {
        return value ? FALSE_GEN : TRUE_GEN;
    }
    
    /** @see Object#toString() */
    @Override
    public final String toString() {
        return value ? "1" : "0";
    }

    /** @see Gen#getValue() */
    public final Boolean getValue() {
        return value;
    }

    /** @see Object#equals(Object) */
    @Override
    public final boolean equals(final Object obj) {
        boolean ret = false;
        
        if(obj instanceof BooleanGen) {
            final BooleanGen b = (BooleanGen) obj;
            ret = b.value == value;
        }
        
        return ret;
    }
    
    /** @see Object#hashCode() */
    @Override
    public final int hashCode() {
        return value ? 1 : 0;
    }
    
    /** dado un caracter (1 o 0) genera el gen correspondiente */
    public static BooleanGen valueOf(final char c) {
        final BooleanGen gen;
        if(c == '1') {
            gen = new BooleanGen(true);
        } else if(c == '0') {
            gen = new BooleanGen(false);
        } else {
            throw new IllegalArgumentException("solo puedo convertir 1 o 0s."
                    + " Se me dio el valor `" + c + "'");
        }
        
        return gen;
    }
    
    /** gen verdadero */
    public static final BooleanGen TRUE_GEN = new BooleanGen(true);
    /** gen falso */
    public static final BooleanGen FALSE_GEN = new BooleanGen(false);
 
    /** dado un booleano retorna el gen */
    public static BooleanGen valueOf(final boolean b) {
        return b ? TRUE_GEN : FALSE_GEN;
    }
}
